package com.microsoft.mcp.sample.client;

import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.http.HttpMcpTransport;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolProvider;

import java.time.Duration;
import java.util.List;
import java.util.Scanner;

public class LangChain4jClient {
        private static final String QWEN_KEY="sk-7a177f83edb44fea98132cab4742e0b8";
        private static final String QWEN_NAME="qwen-turbo-latest";

        /**
         * This example uses the calculator MCP server that provides basic calculator
         * operations.
         * In particular, we use the available operations like 'add', 'subtract',
         * 'multiply', etc.
         * <p>
         * Before running this example, you need to start the calculator server in SSE
         * mode on localhost:8080.
         * <p>
         * Run the example and check the logs to verify that the model used the
         * calculator tools.
         */
        public static void main(String[] args) throws Exception {
                ChatLanguageModel qwenModel = QwenChatModel.builder()
                        .apiKey(QWEN_KEY)
                        .modelName(QWEN_NAME)
                        .build();

                McpTransport transport = new HttpMcpTransport.Builder()
                                .sseUrl("http://localhost:8085/sse")
                                .timeout(Duration.ofSeconds(60))
                                .logRequests(true)
                                .logResponses(true)
                                .build();

                McpClient mcpClient = new DefaultMcpClient.Builder()
                                .transport(transport)
                                .build();

                ToolProvider toolProvider = McpToolProvider.builder()
                                .mcpClients(List.of(mcpClient))
                                .build();

                Bot bot = AiServices.builder(Bot.class)
                                .chatLanguageModel(qwenModel)
                                .toolProvider(toolProvider)
                                .build();
                try {
                        Scanner scanner = new Scanner(System.in);
                        /*String response = bot.chat("Calculate the sum of 24.5 and 17.3 using the calculator service");
                        System.out.println(response);

                        response = bot.chat("What's the square root of 144?");
                        System.out.println(response);

                        response = bot.chat("Show me the help for the calculator service");
                        System.out.println(response);*/
                        while (true){
                                String userInput = scanner.nextLine();
                                String response = bot.chat(userInput);
                                System.out.println(response);
                        }
                } finally {
                        mcpClient.close();
                }
        }
}